package com.app.micaihu.view.main.fragment;

import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.adapter.HotNewsRankAdapter;
import com.app.micaihu.adapter.NewsAdapter;
import com.app.micaihu.adapter.VideoRankAdapter;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.LazyBaseListFragment;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.bean.news.NewsList;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.baidu.mobstat.StatService;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Guo on 2017/7/3.
 *
 * 热点的Fragment
 *
 */

public class HotNewsFragment extends LazyBaseListFragment<NewsEntity>{
    private String mChannelId = "7";
    @Override
    protected void changeData(final boolean isRefresh) {
        HashMap<String, String> parmsMap = new HashMap<String, String>();
        parmsMap.put("type", mChannelId + "");
        parmsMap.put("page", dataPage + "");
        parmsMap.put("mark", " 0");
        HttpListener<DataBean<NewsList>> httpListener = new HttpListener<DataBean<NewsList>>() {

            @Override
            public void onStart() {
                setLoadView(LOADING, null);
            }
            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror));
            }
            @Override
            public void onSuccess(DataBean<NewsList> response) {
                if(response.noErrorData()){
                    NewsList data = response.getData();
                    List<NewsEntity> newsList = data.getNewsList();
                    if(newsList != null && newsList.size() > 0){
                        if (dataList == null) {
                            dataList = new ArrayList<NewsEntity>();
                        }

                        if(isRefresh){
                            dataList.clear();
                        }


                        dataList.addAll(newsList);

                        if (mAdapter == null) {
                            mAdapter = new HotNewsRankAdapter(dataList, getActivity());
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }

                        setLoadView(LOADSUCES, null);

                    }else{
                        if (isRefresh && dataList != null) {
                            dataList.clear();
                        }
                        if (dataList == null || dataList.size() == 0) {
                            setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.pullup_no_data));
                        } else {
                            setLoadView(LOADSUCES, getString(R.string.homepage_msg_nomoremsg));
                        }
                    }
                }else{
                    setLoadView(DATAERROR, response.getNnderstoodMsg());
                }
            }
        };


        addPostCacheRequest(URIContant.NEWSLIST, new TypeToken<DataBean<NewsList>>() {
        }.getType(), parmsMap, httpListener);

    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //因为有头部
        if (mListView.getRefreshableView().getHeaderViewsCount() > 0) {
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        //防止越界
        if (position >= dataList.size()) {
            position = dataList.size() - 1;
        } else if (position < 0) {
            position = 0;
        }

        StatService.onEvent(activity, "055", "内容榜_文章", 1);// 统计的点击的次数
        NewsEntity news = dataList.get(position);
        if (!TypeConstant.openTypeDetail(news, activity, TypeConstant.SOURCETYPE_NORMAL)) {
            if (news == null) {
                return;
            }
        }
        //添加已读文章id
        TextView textView = (TextView) view.findViewById(R.id.item_public_title);
        if (textView != null) {
            textView.setEnabled(false);
        }
        //添加已读新闻
        NewsAdapter.addReadList(news.getArticleId());
    }
}
